# -*- coding: utf-8 -*-
import animal
import room
import csv


class AnimalHotel:
    def __init__(self):
        self._rooms = []  # 用于存储所有的房间信息
        self._animals = []  # 用于存储已登记入住的动物信息

    def add_room(self):
        id = input("请输入你要添加的房间号：")
        choice = int(input("请输入该房间的种类,'1'表示长方体房间，'2'表示球形房间"))
        if choice == 1:
            length = int(input("请输入该房间的长度"))
            width = int(input("请输入该房间的宽度"))
            height = int(input("请输入该房间的高度"))
            temp_room = room.CubeRoom(id, length, width, height)
            self._rooms.append(temp_room)

        if choice == 2:
            radius = int(input("请输入该房间的半径"))
            temp_room = room.SphereRoom(id, radius)
            self._rooms.append(temp_room)

    def remove_room(self):
        id = input("请输入你要删除的房间号")
        for room in self._rooms:
            if id == room._id:
                self._rooms.remove(room)
                print(id + "删除成功")
            else:
                print("找不到该房间！")

    def add_animal(self, animal):
        self._animals.append(animal)
        pass

    def remove_animal(self, id):
        for animal in self._animals:
            if animal._id == id:
                self._animals.remove(animal)
                print("动物信息已删除！")

    def register_proper_room(self):
        print("开始为您办理入住手续...")
        name = input("请输入动物的名字：")
        age = int(input("请输入动物的年龄"))
        animal_choice = int(
            input("请输入动物的种类，'1'表示哺乳动物，'2'表示鸟类动物，'3'表示爬行动物，'4'表示两栖动物，'5'表示鱼类动物"))
        if animal_choice == 1:
            length = int(input("请输入动物的长度："))
            width = int(input("请输入动物的宽度："))
            height = int(input("请输入动物的高度："))
            animal_v = float(length * width * height)
            for room in self._rooms:
                if room._is_free == True and room.get_room_type() == "CubeRoom" and float(
                        room._length * room._width * room._height) >= animal_v:
                    animal_temp = animal.Mammal(name, age, room._id, length, width, height)
                    self.add_animal(animal_temp)
                    room.set_password()
                    room.set_room_occupied()
                    animal_temp.call()
                    print("入住办理成功！")
                else:
                    print("没有找到合适的房间呢！")
        if animal_choice == 2:
            length = int(input("请输入动物的长度："))
            width = int(input("请输入动物的宽度："))
            height = int(input("请输入动物的高度："))
            animal_size = max(length, width, height)
            for room in self._rooms:
                if room._is_free == True and room.get_room_type() == "SphereRoom" and room.get_radius() >= animal_size:
                    animal_temp = animal.Bird(name, age, room._id, length, width, height)
                    self.add_animal(animal_temp)
                    room.set_password()
                    room.set_room_occupied()
                    animal_temp.call()
                    print("入住办理成功！")
                else:
                    print("没有找到合适的房间呢！")
        if animal_choice == 3:
            length = int(input("请输入动物的长度："))
            width = int(input("请输入动物的宽度："))
            height = int(input("请输入动物的高度："))
            for room in self._rooms:
                if room._is_free == True and room.get_room_type() == "CubeRoom" and room._length >= length and room._width >= width and room._height >= height:
                    animal_temp = animal.Reptile(name, age, room._id, length, width, height)
                    self.add_animal(animal_temp)
                    room.set_password()
                    room.set_room_occupied()
                    animal_temp.call()
                    print("入住办理成功！")
                else:
                    print("没有找到合适的房间呢！")
        if animal_choice == 4:
            length = int(input("请输入动物的长度："))
            width = int(input("请输入动物的宽度："))
            height = int(input("请输入动物的高度："))
            for room in self._rooms:
                if room._is_free == True and room.get_room_type() == "CubeRoom" and room._length >= length and room._width >= width and room._height >= height:
                    animal_temp = animal.Amphibian(name, age, room._id, length, width, height)
                    self.add_animal(animal_temp)
                    room.set_password()
                    room.set_room_occupied()
                    animal_temp.call()
                    print("入住办理成功！")
                else:
                    print("没有找到合适的房间呢！")
        if animal_choice == 5:
            length = int(input("请输入动物的长度："))
            width = int(input("请输入动物的宽度："))
            height = int(input("请输入动物的高度："))
            for room in self._rooms:
                if room._is_free == True and room.get_room_type() == "CubeRoom" and room._length >= length and room._width >= width and room._height >= height:
                    animal_temp = animal.Fish(name, age, room._id, length, width, height)
                    self.add_animal(animal_temp)
                    room.set_password()
                    room.set_room_occupied()
                    animal_temp.call()
                    print("入住办理成功！")
                else:
                    print("没有找到合适的房间呢！")

    def check_out_of_room(self):
        id = input("请输入退房的房间号：")
        self.remove_animal(id)
        for room in self._rooms:
            if room._id == id:
                room.set_room_free()
                room._password = None
                print("房间信息已重置")
        print("退房成功！纳西妲真可爱！！")

    def save_all_room(self):
        with open('rooms.csv', 'w') as f:
            writer = csv.writer(f)
            writer.writerow(['id', 'room_type', 'room_size'])
            for room in self._rooms:
                if room.get_room_type() == "CubeRoom":
                    writer.writerow([str(room._id), "CubeRoom", "length="+str(room._length)+","+"width="+
                                     str(room._width)+","+"height="+str(room._height)])
                if room.get_room_type() == "SphereRoom":
                    writer.writerow([str(room._id), "SphereRoom", str(room.get_radius())])

    def get_all_rooms_from_csv(self):
        with open('rooms.csv', 'r') as f:
            reader = csv.reader(f)
            for line in reader:
                print(reader.line_num, end='\t')
                for elem in line:
                    print(elem, end='\t')
                print()

    def view_all_rooms(self):
        free_rooms = []
        occupied_rooms = []
        for room in self._rooms:
            if room._is_free == True:
                free_rooms.append(room._id)
            if room._is_free == False:
                occupied_rooms.append((room._id))
        print("空房间有", end=" ")
        for i in free_rooms:
            print(i)
        print("已入住的房间有", end=" ")
        for j in occupied_rooms:
            print(j)


def main():
    pass


if __name__ == "__main__":
    main()
